//1111111111
//  SetViewController.m
//  Text_ problem
//
//  Created by jiboxue on 13-1-9.
//  Copyright (c) 2013年 jiboxue. All rights reserved.
//

#define kAlphaNum   @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
#define kAlpha      @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz "
#define kNumbers     @"0123456789"
#define kNumbersPeriod  @"0123456789."

#define DOCUMENT_FOLDER	   [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]

#define bgImageKey @"bgImageKey"
#define okImageFramKey @"okImageFramKey"
#define bigOkImageFramKey @"bigOkImageFramKey"

#import "SetViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "RootViewController.h"

#import "PMAPI.h"
#import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h"
#import "json.h"
#import "CustomerInformation.h"

#import "UIImageView+WebCache.h"

@interface SetViewController ()

@property (nonatomic, retain) ASIHTTPRequest *changePasswordRequest;

@end

@implementation SetViewController

@synthesize popoverController;

@synthesize textFieldName = _textFieldName;
@synthesize textFieldOldpassword = _textFieldOldpassword;
@synthesize textFieldNewpassword = _textFieldNewpassword;
@synthesize textFieldSurepassword = _textFieldSurepassword;

@synthesize delegate;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        appD = [UIApplication sharedApplication].delegate;
        self.imageView_hook = [[[UIImageView alloc]init]autorelease];
    }
    return self;
}
-(void)playSound:(NSString*)soundKey{
	
	NSString *path = [NSString stringWithFormat:@"%@%@",[[NSBundle mainBundle] resourcePath],soundKey];
    
	SystemSoundID soundID;
	
	NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
	
	AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID);
	
	AudioServicesPlaySystemSound(soundID);
	
}
- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor clearColor];
    [self Threelivebutton];
    
    UIImageView *imageView_round = [[UIImageView alloc]initWithFrame:CGRectMake(223.5, 96.5, 577, 575)];
    imageView_round.userInteractionEnabled = YES;
    imageView_round.image = [UIImage imageNamed:@"circel_background_blank.png"];
    [self.view addSubview:imageView_round];
    
    imageView_account = [[UIImageView alloc]initWithFrame:CGRectMake(47, 46, 483, 483)];
    imageView_account.userInteractionEnabled = YES;
    imageView_account.image = [UIImage imageNamed:@"setting_avatar.png"];
    [imageView_round addSubview:imageView_account];
    
    self.imageViewHead = [[[UIImageView alloc]initWithFrame:CGRectMake(130, 80, 233, 230)]autorelease];
    //把图片变成圆形
    [self.imageViewHead.layer setCornerRadius:self.imageViewHead.frame.size.width / 2];
    [self.imageViewHead.layer setMasksToBounds:YES];
    [self.imageViewHead.layer setBorderWidth:0];
    [imageView_account addSubview:self.imageViewHead];
    
    UIButton *button_imageView_head = [UIButton buttonWithType:UIButtonTypeCustom];
    button_imageView_head.frame = CGRectMake(140, 100, 200, 200);
    [button_imageView_head addTarget:self action:@selector(buttonImageViewHead:) forControlEvents:UIControlEventTouchUpInside];
    [imageView_account addSubview:button_imageView_head];
    
    UIImageView *imageView_head_view = [[UIImageView alloc]initWithFrame:CGRectMake(120, 70, 280, 251)];
    imageView_head_view.image = [UIImage imageNamed:@"setting_avatar_cover.png"];
    [imageView_account addSubview:imageView_head_view];
    [imageView_head_view release];
    
    UITextField *textField_name = [[UITextField alloc]initWithFrame:CGRectMake(90, 345, 300, 40)];
    textField_name.borderStyle = UITextBorderStyleRoundedRect;
    //键盘类型
    textField_name.placeholder = @"Change your nick name here";
    textField_name.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    textField_name.clearButtonMode = YES;
    textField_name.delegate = self;
    self.textFieldName = textField_name;
    [imageView_account addSubview:self.textFieldName];
    
    UIButton *button_ChangeNameHeadImage = [UIButton buttonWithType:UIButtonTypeCustom];
    button_ChangeNameHeadImage.frame = CGRectMake(198, 398, 70, 70 );
    [button_ChangeNameHeadImage setImage:[UIImage imageNamed:@"setting_nickname_ok.png"] forState:UIControlStateNormal];
    [button_ChangeNameHeadImage setImage:[UIImage imageNamed:@"setting_nickname_ok_down.png"] forState:UIControlEventTouchUpOutside];
    [button_ChangeNameHeadImage addTarget:self action:@selector(buttonChangeNameHeadImage) forControlEvents:UIControlEventTouchUpInside];
    [imageView_account addSubview:button_ChangeNameHeadImage];
    //隐藏..............................................
    imageView_account.alpha = 1;
    [textField_name release];
    
    //第二个按钮@@@@@@@@@@@@@@@@@@@
    imageView_password = [[UIImageView alloc]initWithFrame:CGRectMake(46, 45, 480, 480)];
    //隐藏..............................................
    imageView_password.alpha = 0;
    imageView_password.image = [UIImage imageNamed:@"setting_password_background.png"];
    [imageView_round addSubview:imageView_password];
        
    UITextField *textField_oldpassword = [[UITextField alloc]initWithFrame:CGRectMake(118, 123, 257, 46)];
    //textfield圆角
    textField_oldpassword.borderStyle = UITextBorderStyleRoundedRect;
    //字体大小
    textField_oldpassword.font = [UIFont systemFontOfSize:15.0];
    //字体居中
    textField_oldpassword.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    //密码加密
    [textField_oldpassword setSecureTextEntry:YES];
    textField_oldpassword.keyboardType = UIKeyboardTypeNumberPad;//////
    textField_oldpassword.returnKeyType = UIReturnKeyDone;
    //提示
    textField_oldpassword.placeholder = @"Old Password";
    textField_oldpassword.clearButtonMode = YES;
    textField_oldpassword.delegate = self;
    self.textFieldOldpassword = textField_oldpassword;
    [imageView_password addSubview:self.textFieldOldpassword];
    [textField_oldpassword release];
    
    UITextField *textField_newpassword = [[UITextField alloc]initWithFrame:CGRectMake(118, 192, 257, 46)];
    //textfield圆角
    textField_newpassword.borderStyle = UITextBorderStyleRoundedRect;
    //字体大小
    textField_newpassword.font = [UIFont systemFontOfSize:15.0];
    //字体居中
    textField_newpassword.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    //密码加密
    [textField_newpassword setSecureTextEntry:YES];
    textField_newpassword.keyboardType = UIKeyboardTypeNumberPad;
    //提示
    textField_newpassword.placeholder = @"New Password";
    textField_newpassword.clearButtonMode = YES;
    textField_newpassword.delegate = self;
    self.textFieldNewpassword = textField_newpassword;
    [imageView_password addSubview:self.textFieldNewpassword];
    [textField_newpassword release];
    
    UITextField *textField_surepassword = [[UITextField alloc]initWithFrame:CGRectMake(118, 262, 257, 46)];
    //textfield圆角
    textField_surepassword.borderStyle = UITextBorderStyleRoundedRect;
    //字体大小
    textField_surepassword.font = [UIFont systemFontOfSize:15.0];
    //字体居中
    textField_surepassword.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    //密码加密
    [textField_surepassword setSecureTextEntry:YES];
    textField_surepassword.keyboardType = UIKeyboardTypeNumberPad;
    //提示
    textField_surepassword.placeholder = @"Comfirm Password";
    textField_surepassword.clearButtonMode = YES;
    textField_surepassword.delegate = self;
    self.textFieldSurepassword = textField_surepassword;
    [imageView_password addSubview:self.textFieldSurepassword];
    [textField_surepassword release];
    
    UIButton *button_ok = [UIButton buttonWithType:UIButtonTypeCustom];
    [button_ok setBackgroundImage:[UIImage imageNamed:@"btn_confirm.png"] forState:UIControlStateNormal];
    button_ok.frame = CGRectMake(120, 330, 117, 47);
    [button_ok addTarget:self action:@selector(confirm) forControlEvents:UIControlEventTouchUpInside];
    [imageView_password addSubview:button_ok];
    
    UIButton *button_cancel = [UIButton buttonWithType:UIButtonTypeCustom];
    [button_cancel setBackgroundImage:[UIImage imageNamed:@"btn_cancel.png"] forState:UIControlStateNormal];
    button_cancel.frame = CGRectMake(255, 330, 117, 47);
    [button_cancel addTarget:self action:@selector(button_cancel) forControlEvents:UIControlEventTouchUpInside];
    [imageView_password addSubview:button_cancel];
    
    imageView_lock_under = [[UIImageView alloc]initWithFrame:CGRectMake(4, 242, 475, 235)];
    imageView_lock_under.image = [UIImage imageNamed:@"setting_password_down.png"];
    [imageView_password addSubview:imageView_lock_under];
    
    imageView_lock_on = [[UIImageView alloc]initWithFrame:CGRectMake(4, 7, 475, 326)];
    imageView_lock_on.image = [UIImage imageNamed:@"setting_password_up.png"];
    [imageView_password addSubview:imageView_lock_on];
    
    button_image_animation = [UIButton buttonWithType:UIButtonTypeCustom];
    button_image_animation.frame = CGRectMake(235, 240, 100, 100);
    [button_image_animation addTarget:self action:@selector(btuttonAnimation) forControlEvents:UIControlEventTouchUpInside];
    [imageView_round addSubview:button_image_animation];
    //超出俯视图不显示
    imageView_password.clipsToBounds = YES;
    //用layer做一个圆角
    imageView_password.layer.cornerRadius = 240;
    
    //第三个
    imageView_background = [[UIImageView alloc]initWithFrame:CGRectMake(47, 46, 483, 483)];
    imageView_background.alpha = 0;
    [imageView_round addSubview:imageView_background];
    UIButton *button_blue = [UIButton buttonWithType:UIButtonTypeCustom];
    [button_blue setBackgroundImage:[UIImage imageNamed:@"setting_background_2.png"] forState:UIControlStateNormal];
    [button_blue addTarget:self action:@selector(buttonBlue) forControlEvents:UIControlEventTouchUpInside];
    button_blue.frame = CGRectMake(160, 60, 156, 167);
    imageView_background.userInteractionEnabled = YES;
    [imageView_background addSubview:button_blue];
    [imageView_background release];
    UIButton *button_white = [UIButton buttonWithType:UIButtonTypeCustom];
    [button_white setBackgroundImage:[UIImage imageNamed:@"setting_background_1.png"] forState:UIControlStateNormal];
    [button_white addTarget:self action:@selector(buttonWhite) forControlEvents:UIControlEventTouchUpInside];
    button_white.frame = CGRectMake(60, 240, 156, 167);
    imageView_background.userInteractionEnabled = YES;
    [imageView_background addSubview:button_white];
    
    UIButton *button_pink = [UIButton buttonWithType:UIButtonTypeCustom];
    [button_pink setBackgroundImage:[UIImage imageNamed:@"setting_background_3.png"] forState:UIControlStateNormal];
    [button_pink addTarget:self action:@selector(buttonPink) forControlEvents:UIControlEventTouchUpInside];
    button_pink.frame = CGRectMake(270, 240, 156, 167);
    imageView_background.userInteractionEnabled = YES;
    [imageView_background addSubview:button_pink];
    
    self.imageView_hook.image = [UIImage imageNamed:@"setting_tick.png"];
    [imageView_background addSubview:self.imageView_hook];

    NSString* str = [[NSUserDefaults standardUserDefaults]stringForKey:bgImageKey];
    if (str != nil)
    {
        self.imageView_hook.frame = CGRectFromString(
                                                      [[NSUserDefaults standardUserDefaults] stringForKey:bigOkImageFramKey]);
    }
    else
    {
        self.imageView_hook.frame = CGRectMake(225, 130, 27, 21);
    }
        
    [imageView_round release];
    
}
////只能输入数字或字母
//- (BOOL)textField:(UITextField *)textField
//shouldChangeCharactersInRange:(NSRange)range
//replacementString:(NSString *)string {
//    
//    NSCharacterSet *cs;
//    cs = [[NSCharacterSet characterSetWithCharactersInString:kAlphaNum] invertedSet];
//    NSString *filtered =
//    [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
//    BOOL basic = [string isEqualToString:filtered];
//    return basic;


//}
-(void)Threelivebutton
{
    imageView_button = [[UIImageView alloc]initWithFrame:CGRectMake(50, 301.5, 370, 165)];
    imageView_button.image = [UIImage imageNamed:@"setting_menu1.png"];
    imageView_button.userInteractionEnabled = YES;
    [self.view addSubview:imageView_button];
    
    UIButton *button_account = [UIButton buttonWithType:UIButtonTypeCustom];
    button_account.frame = CGRectMake(0, 0, 230, 55);
    [button_account addTarget:self action:@selector(account) forControlEvents:UIControlEventTouchUpInside];
    [imageView_button addSubview:button_account];
    
    UIButton *button_password = [UIButton buttonWithType:UIButtonTypeCustom];
    button_password.frame = CGRectMake(0, 55, 230, 55);
    [button_password addTarget:self action:@selector(password) forControlEvents:UIControlEventTouchUpInside];
    [imageView_button addSubview:button_password];
    
    UIButton *button_background = [UIButton buttonWithType:UIButtonTypeCustom];
    button_background.frame = CGRectMake(0, 110, 230, 55);
    [button_background addTarget:self action:@selector(background) forControlEvents:UIControlEventTouchUpInside];
    [imageView_button addSubview:button_background];
}
-(void)buttonChangeNameHeadImage
{
    soundFile = [NSString stringWithFormat:@"/btn_big.mp3"];
    [self playSound: soundFile];
    
    if ([_textFieldName.text length] != 0)
    {
//        [self.delegate setTextField:_textFieldName.text andImageView:_imageViewHead.image];
        [self.delegate setTextField:_textFieldName.text];
        NSDictionary *dictionaryUserInfo = [CustomerInformation userMessage];
        NSString *username = [dictionaryUserInfo objectForKey:@"username"];
        NSLog(@"%@",username);
        NSString *password = [dictionaryUserInfo objectForKey:@"password"];
        NSLog(@"%@",password);
        ASIFormDataRequest *request = [PMAPI requestModifyName:_textFieldName.text andUsername:username andPassword:password];
        request.delegate =self;
        [request startAsynchronous];
        NSLog(@"11");
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:@"Succeed" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];

        _textFieldName.text = nil;
    }
    else
    {
        [self showInfo1:@"Please enter your nick name"];
        NSLog(@"22");
    }
}
//照相按钮
-(void)buttonImageViewHead:(id)sender
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"change profile photo" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"camera" otherButtonTitles:@"photo ablums", nil];
    actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
    [actionSheet showInView:self.view];
    [actionSheet release];
    NSLog(@"11");
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        NSLog(@"11");
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
            [imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
            [imagePicker setDelegate:self];
            UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
            self.popoverController = popover;
            [self.popoverController presentPopoverFromRect:CGRectMake(880, 620, 50, 50) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
            [popover release];
        }else
        {
            [self showInfo1:@"camera disabled"];
        }
        [imagePicker release];
    }
    else if (buttonIndex == 1) {
        NSLog(@"22");
        if ([UIImagePickerController isSourceTypeAvailable:
             UIImagePickerControllerSourceTypePhotoLibrary]) {
            UIImagePickerController *m_imagePicker = [[UIImagePickerController alloc] init];
            m_imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            m_imagePicker.allowsEditing = YES;
            m_imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
            m_imagePicker.delegate = self;
            UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:m_imagePicker];
            self.popoverController = popover;
            [self.popoverController presentPopoverFromRect:CGRectMake(880, 620, 50, 50) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
            [popover release];
            [m_imagePicker release];
        }else {
            [self showInfo1:@"camera disabled"];
            
        }
    }
}
//把编辑后的图片拿到
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    if (picker.allowsEditing == YES)
    {
        image = [info objectForKey:@"UIImagePickerControllerEditedImage"];
    }
    self.imageViewHead.image = image;
    //如果scourcetype是相机,需要保存照片
    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

    
    NSData *imageData = UIImageJPEGRepresentation(self.imageViewHead.image, 0.4);
    
    NSString *fileName = [NSString stringWithFormat:@"%@", [[self class] GetUUID]];
    NSString *filePath = [[[self class] getImagePathInDocument] stringByAppendingPathComponent:fileName];
    
    BOOL bRes = [imageData writeToFile:filePath atomically:YES];
    
    if (bRes) {
        NSDictionary *dictionaryUserInfoModifyName = [CustomerInformation userMessage];
        NSString *username = [dictionaryUserInfoModifyName objectForKey:@"username"];
        NSLog(@"解析出来的userid = %@",username);
        NSString *password = [dictionaryUserInfoModifyName objectForKey:@"password"];
        NSLog(@"%@",password);
        
        [self showInfo1:@"succeed"];
        
        if (username)
        {
            ASIHTTPRequest *request = [PMAPI requestHeadImage:filePath andUsername:username andPassword:password];
            
            [request startSynchronous];
            
            NSLog(@"%d",[request responseStatusCode]);
            
            int responseCode = request.responseStatusCode;
            if (responseCode == 401) {
                [self showInfo1:@"User name and password do not match"];

            }
            
            if ([request error]) {
                [self showInfo1:@"Internet Failure"];
            }
            else if (responseCode != 200)
            {
                [self showInfo1:@"request failed"];
            }
            else {
            
            //如果scourcetype是相机,需要保存照片
            [self.popoverController dismissPopoverAnimated:YES];//关掉当前视图
            picker = nil;
            appD.rootVC.image_head.image = self.imageViewHead.image;
            }
            
            
        }
    }

}
-(void)showInfo1:(NSString *)textMessage
{
    UIAlertView *alertView =
    [[UIAlertView alloc] initWithTitle:@"Alert"
                               message:textMessage
                              delegate:self
                     cancelButtonTitle:@"OK"
                     otherButtonTitles:nil];
    
    [alertView show];
    [alertView release];
}

+ (NSString *)GetUUID
{
    CFUUIDRef theUUID = CFUUIDCreate(NULL);
    CFStringRef string = CFUUIDCreateString(NULL, theUUID);
    CFRelease(theUUID);
    return [(NSString *)string autorelease];
}

+ (NSString *)getImagePathInDocument {
    NSString *localFilePath = [DOCUMENT_FOLDER stringByAppendingPathComponent:@"Image"];
    
    if (![[NSFileManager defaultManager] fileExistsAtPath:localFilePath]) {
        NSError *error;
        [[NSFileManager defaultManager] createDirectoryAtPath:localFilePath withIntermediateDirectories:NO attributes:nil error:&error];
    }
    
    return localFilePath;
}

-(void)cun
{
    [[NSUserDefaults standardUserDefaults]setValue:NSStringFromCGRect(appD.rootVC.image_background_ok.frame) forKey:okImageFramKey];
    [[NSUserDefaults standardUserDefaults]setValue:NSStringFromCGRect(self.imageView_hook.frame) forKey:bigOkImageFramKey];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
#pragma mark - 背景的更换
-(void)buttonPink
{
    soundFile = [NSString stringWithFormat:@"/btn_small.mp3"];
    [self playSound: soundFile];
    
    appD.rootVC.background.image = [UIImage imageNamed:@"bg_red.png"];
    self.imageView_hook.frame = CGRectMake(335, 310, 27, 21);
    appD.rootVC.image_background_ok.frame = CGRectMake(202, 370, 27, 21);
    [[NSUserDefaults standardUserDefaults]setObject:@"bg_red.png" forKey:bgImageKey];
    [self cun];
}
-(void)buttonWhite
{
    soundFile = [NSString stringWithFormat:@"/btn_small.mp3"];
    [self playSound: soundFile];
    
    appD.rootVC.background.image = [UIImage imageNamed:@"bg_write.png"];
    self.imageView_hook.frame = CGRectMake(125, 310, 27, 21);
    appD.rootVC.image_background_ok.frame = CGRectMake(163, 306, 27, 21);
    [[NSUserDefaults standardUserDefaults]setObject:@"bg_write.png" forKey:bgImageKey];
    [self cun];
}
-(void)buttonBlue
{
    soundFile = [NSString stringWithFormat:@"/btn_small.mp3"];
    [self playSound: soundFile];
    
    appD.rootVC.background.image = [UIImage imageNamed:@"bg_blue.png"];
    self.imageView_hook.frame = CGRectMake(225, 130, 27, 21);
    appD.rootVC.image_background_ok.frame = CGRectMake(122, 373, 27, 21);
    [[NSUserDefaults standardUserDefaults]setObject:@"bg_blue.png" forKey:bgImageKey];
    [self cun];    
}
#pragma mark - 所的动画
-(void)btuttonAnimation
{
    soundFile = [NSString stringWithFormat:@"/lock.mp3"];
    [self playSound: soundFile];

    button_image_animation.alpha = 0;
    imageView_password.userInteractionEnabled = YES;
    imageView_lock_on.image = [UIImage imageNamed:@"setting_password_up_open.png"];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1.0f];
    imageView_lock_on.frame = CGRectMake(4, -500, 475, 326);
    imageView_lock_under.frame = CGRectMake(4, 700, 475, 235);
    [UIView commitAnimations];
    
    NSLog(@"www");
}
-(void)button_cancel
{
    //调用键盘小时的方法
    [self Keyboard_disappear];
    soundFile = [NSString stringWithFormat:@"/lock.mp3"];
    [self playSound: soundFile];
    button_image_animation.alpha = 1;
    imageView_password.userInteractionEnabled = NO;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1.0f];
    imageView_lock_on.image = [UIImage imageNamed:@"setting_password_up.png"];
    imageView_lock_on.frame = CGRectMake(4, 7, 475, 326);
    imageView_lock_under.frame = CGRectMake(4, 242, 475, 235);
    [UIView commitAnimations];

}
-(void)confirm
{
    
    if ([_textFieldOldpassword.text length] == 0 ||
        _textFieldOldpassword.text == Nil)
    {
        [self showInfo1:@"Old password can't be empty"];
    }
    else if([_textFieldNewpassword.text length] == 0 ||
            _textFieldNewpassword.text == Nil)
    {
        [self showInfo1:@"New password can't be empty"];
    }
    else if([_textFieldNewpassword.text isEqualToString:_textFieldSurepassword.text] != YES){
    [self showInfo1:@"please enter the same new passwords"];
    }
    else if ([_textFieldNewpassword.text isEqualToString:@"0"])
    {
        [self showInfo1:@"New password can't be 0"];
    }
//    else if(![_textFieldNewpassword.text isEqualToString:[[_textFieldNewpassword.text componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:kNumbers] invertedSet]] componentsJoinedByString:@""]])
//    {
//        [self showInfo1:@"The new password can't contain special characters"];
//    }
    else{
        NSDictionary *dictionaryUserInfoModifyName = [CustomerInformation userMessage];
//        NSString *username = [dictionaryUserInfoModifyName objectForKey:@"username"];
        NSString *userid = [dictionaryUserInfoModifyName objectForKey:@"id"];
        NSLog(@"解析出来的userid = %@",userid);
//        NSString *password = [dictionaryUserInfoModifyName objectForKey:@"password"];
//        NSLog(@"%@",password);
        
        ASIFormDataRequest *request = [PMAPI requestChangeNewPassword:_textFieldSurepassword.text andOldPassword:_textFieldOldpassword.text andUserID:userid];
        request.delegate =self;
        
        self.changePasswordRequest = request;
        
        self.PasswordNewText = _textFieldSurepassword.text;
        
        [request startAsynchronous];
    }
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
    if ([request responseStatusCode] != 200) {
        [self showInfo1:@"old password is wrong"];
    }
    else
    {
        NSMutableDictionary *dictionaryMessage = [NSMutableDictionary dictionaryWithDictionary:[CustomerInformation userMessage]];
        
        if ([self.PasswordNewText length] != 0) {
            [dictionaryMessage setObject:self.PasswordNewText
                                  forKey:@"password"];
        }
        
        [CustomerInformation saveUserMessage:dictionaryMessage];
        
        //调用键盘小时的方法
        [self Keyboard_disappear];
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"Succeed" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
        
        button_image_animation.alpha = 1;
        imageView_password.userInteractionEnabled = NO;
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:1.0f];
        imageView_lock_on.image = [UIImage imageNamed:@"setting_password_up.png"];
        imageView_lock_on.frame = CGRectMake(4, 7, 475, 326);
        imageView_lock_under.frame = CGRectMake(4, 242, 475, 235);
        [UIView commitAnimations];
    }
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
    [self showInfo1:@"Internet Failure"];
}

- (void)Keyboard_disappear
{
    [_textFieldOldpassword resignFirstResponder];
    [_textFieldNewpassword resignFirstResponder];
    [_textFieldSurepassword resignFirstResponder];
    
    _textFieldOldpassword.text = nil;
    _textFieldNewpassword.text = nil;
    _textFieldSurepassword.text = nil;
}
#pragma mark - textfield的代理方法
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    if (orientation == UIInterfaceOrientationLandscapeLeft) {
        appD.rootVC.view.frame = CGRectMake(-170, 0, 768, 1024);
        
    }
    else if (orientation == UIInterfaceOrientationLandscapeRight)
    {
        appD.rootVC.view.frame = CGRectMake(170, 0, 768, 1024);
    }

}
- (void)textFieldDidEndEditing:(UITextField *)textField
{    
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    if (orientation == UIInterfaceOrientationLandscapeLeft) {
        appD.rootVC.view.frame = CGRectMake(0, 0, 768, 1024);
        
    }
    else if (orientation == UIInterfaceOrientationLandscapeRight)
    {
        appD.rootVC.view.frame = CGRectMake(0, 0, 768, 1024);
    }
    
}
#pragma mark - 账户,密码,背景
-(void)account
{
    
    [_textFieldOldpassword resignFirstResponder];
    [_textFieldNewpassword resignFirstResponder];
    [_textFieldSurepassword resignFirstResponder];
    
    soundFile = [NSString stringWithFormat:@"/btn_small.mp3"];
    [self playSound: soundFile];
    
    imageView_button.image = [UIImage imageNamed:@"setting_menu1.png"];
    imageView_account.alpha = 1;
    imageView_password.alpha = 0;
    imageView_background.alpha = 0;
    
}
-(void)password
{
    [self.textFieldName resignFirstResponder];
    
    soundFile = [NSString stringWithFormat:@"/btn_small.mp3"];
    [self playSound: soundFile];
    
    imageView_button.image = [UIImage imageNamed:@"setting_menu2.png"];
    imageView_account.alpha = 0;
    imageView_password.alpha = 1;
    imageView_background.alpha = 0;
}
-(void)background
{
    [self.textFieldName resignFirstResponder];
    [_textFieldOldpassword resignFirstResponder];
    [_textFieldNewpassword resignFirstResponder];
    [_textFieldSurepassword resignFirstResponder];
    
    soundFile = [NSString stringWithFormat:@"/btn_small.mp3"];
    [self playSound: soundFile];
    
    imageView_button.image = [UIImage imageNamed:@"setting_menu3.png"];
    imageView_account.alpha = 0;
    imageView_password.alpha = 0;
    imageView_background.alpha = 1;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
-(void)dealloc
{
    [_textFieldSurepassword release];
    [_textFieldNewpassword release];
    [_textFieldOldpassword release];
    [_textFieldName release];
    self.imageViewHead = nil;
    self.imageView_hook = nil;
    
    [self.changePasswordRequest clearDelegatesAndCancel];
    self.changePasswordRequest = nil;
    self.PasswordNewText = nil;
    
    [super dealloc];
}
@end
